package com.jk.hearthstone;

import com.jk.hearthstone.card.biology.entourage.法力浮龙;
import com.jk.hearthstone.card.biology.hero.*;
import com.jk.hearthstone.card.magic.*;
import com.jk.hearthstone.data.Action;
import com.jk.hearthstone.data.Desktop;
import com.jk.hearthstone.data.History;
import com.jk.hearthstone.data.Turn;
import com.jk.hearthstone.core.Controller;
import com.jk.hearthstone.core.Operator;
import com.jk.hearthstone.core.Producer;

import static com.jk.hearthstone.enumeration.ActionType.ACTION_TYPE_PLAY;


/**
 * @author jk
 */
public class Main {

    static History history = new History();

    public static void main(String[] args) throws CloneNotSupportedException {
        Desktop desktop = new Desktop();
        Controller controller = init(desktop);
        dfs(controller);
    }

    /**
     * 初始化环境
     */
    public static Controller init(Desktop desktop){
        Controller controller = new Controller(desktop,history);
        desktop.enemy = new 敌人();
        desktop.enemy.hp = 10;
        desktop.myself = new 法师();
        desktop.myself.power = 4;
        desktop.myself.hp = 30;
        desktop.myself.canSkill = true;

        desktop.myEntourage.add(new 法力浮龙(controller));
        desktop.myCards.add(new 寒冰箭());
        desktop.myCards.add(new 冰枪术());
        desktop.myCards.add(new 寒冰箭());
        history.currentTurn = new Turn();
        return controller;
    }


    public static void dfs(Controller controller) throws CloneNotSupportedException {
        Turn turn = Producer.getCurrentAction(controller.getDesktop());
        log(turn,"当前所有操作：");
        //深度克隆一份controller的副本，以便回溯时回到初始状态
        Controller spareController = (Controller) controller.clone();
        //出口
        if(controller.getDesktop().enemy.hp<=0){
            log(history.currentTurn,"成功");
        }
        //收工了
        if(turn.actions.size()==0){
            log(history.currentTurn,"回溯");
            return;
        }
        int index=0;
        for (;index<turn.actions.size();index++) {
            Action action = turn.actions.get(index);
            if(!Operator.execute(controller,action)){
                log(history.currentTurn,"操作被拒绝，回溯");
                //将备份的desktop拿回，回到原始状态
                controller = (Controller) spareController.clone();
                back(action);
                continue;
            }
            System.out.println("水晶数："+controller.getDesktop().myself.power);
            System.out.println("敌方血量："+controller.getDesktop().enemy.hp);
            log(history.currentTurn,"出牌顺序");
            Controller dfsController = (Controller) controller.clone();
            dfs(dfsController);
            //将备份的desktop拿回，回到原始状态
            controller = (Controller) spareController.clone();
            turn = Producer.getCurrentAction(controller.getDesktop());
            back(action);
            log(history.currentTurn,"回溯");
        }
    }


    /**
     * 回溯方法
     * 主要功能是清除上一步的历史记录 desktop的回退由克隆完成
     * @param action
     */
    private static void back(Action action){
        //如果是出牌
        if(action.actionType.equals(ACTION_TYPE_PLAY)){
            history.currentTurn.playNum -= 1;
        }
        //清除历史记录
        if(history.currentTurn.actions.size()>0){
            history.currentTurn.actions.remove(history.currentTurn.actions.size()-1);
        }
    }

    private static void log(Turn turn,String info){
        System.out.println(info);
        turn.print();
        System.out.println("-------------------------------------");
    }

}
